home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Creating a pointer to a function "void (*ptrFunction)()" inside a class
- Date: 17 Jan 1996 08:14:52 GMT
- Organization: self-employed
- Message-ID: <4dib5s$92t@news.bridge.net>
- References: <30ECA10F.3D99@ifu.net> <4crkle$h4r@gold.datalytics.com>
- NNTP-Posting-Host: ppp-mia1-38.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
- Jason;
-
- > I am trying to create a base class that has a function that the
- >derived class needs to create. In order to do this, I want to create a
- >pointer to a function in the base class that the derived class can then
- >define. This is not a case where an override will work because the
- >number and names of the derived functions is not known.
-
-
- Jason, quite apart from the issue of the object address, you simply
- cannot assign the address of a function taking two ints, to a pointer to
- function taking void, as your code attempted:
-
- >> int myFunction(int, int);
- >> int (*ptrFunction)();
- >> ptrFunction = myFunction;
-
- If your compiler allows this, it is seriously deficient. C++
- distinguishes all functions, member or nonmember, by their parameter type
- list as well as everything else. The name-mangling, contrary to what
- Robert said, is applied to ALL function names to encode this information.
-
- Member functions do indeed have a hidden parameter, the object's
- address.You can declare a pointer to base class member function in
- your class like so;
-
-
- class BaseClass
- {
- int (BaseClass::*ptrFunction)();
- }
-
-
- You can call it, to operate on an object, like so;
-
-
- (myObject .* ptrFunction) ( parameters )
-
- But, since your one is itself a **member of a class object**, you will
- call it like so:
-
- (myObject .* (yourObject.ptrFunction) ) ( parameters )
-
- It is interesting that the object in which the pointer resides, can be
- different from the object on which you call the function, as I show here.
- You probably don't need this flexibility so you will probably write a
- member function to encapsulate all this nasty stuff.
-
-
- You can indeed call through the pointer in a derived class object, and
- **as far as I can remember** the derived class function will work
- correctly as long as the object is indeed of the correct derived class
- for it. That makes sense to me, but don't take my word for it, I should
- really go and look it up. But it's 3 am. :)
-
- David
-
-
-